package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.laws.LindiLawsQueryVO;
import com.minimalist.common.entity.lindi.vo.laws.LindiLawsVO;
import com.minimalist.common.feign.client.lindi.LindiLawsFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * 林地法规 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiLawsFeignClientFallbackFactory extends BaseFallbackFactory<LindiLawsFeignClient> {

    private static final Logger log = LoggerFactory.getLogger(LindiLawsFeignClientFallbackFactory.class);

    @Override
    public LindiLawsFeignClient create(Throwable cause) {
        return new LindiLawsFeignClient() {
            @Override
            public R<?> addLaws(LindiLawsVO lawsVO) {
                logServiceUnavailable("添加法规", lawsVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "添加法规失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateLawsByLawsId(LindiLawsVO lawsVO) {
                logServiceUnavailable("修改法规", lawsVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "修改法规失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteLawsByLawsId(Long lawsId) {
                logServiceUnavailable("删除法规", lawsId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "删除法规失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiLawsVO> getLawsByLawsId(Long lawsId) {
                logServiceUnavailable("获取法规详情", lawsId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取法规详情失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiLawsVO>> getPageLawsList(LindiLawsQueryVO queryVO) {
                logServiceUnavailable("分页查询法规列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "分页查询法规列表失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}